package com.shuyuntu.common.controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.springframework.stereotype.Controller;

import com.mysql.jdbc.Driver;
import com.mysql.jdbc.Statement;
import com.shuyuntu.controller.Dogcontroller;
import com.shuyuntu.entity.Dog;

@Controller

public class JdbcController {

	/**
	 * 获取连接，加载jdbc
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception {

		String driverClass = "com.mysql.jdbc.Driver";
		String jdbcUrl = "jdbc:mysql://localhost:3306/test";
		String user = "root";
		String password = "root";
		// 读取类路径下的manage.properties
		// InputStream in =
		// getClass().getClassLoader().getResourceAsStream("jdbc.properties");
		// Properties properties = new Properties();
		// properties.load(in);
		// driverClass = properties.getProperty("driver");
		// jdbcUrl = properties.getProperty("jdbcUrl");
		// user = properties.getProperty("user");
		// password = properties.getProperty("password");
		// 通过反射创建Driver实现类对象
		Driver driver = (Driver) Class.forName(driverClass).newInstance();
		Properties pr = new Properties();
		pr.put("user", user);
		pr.put("password", password);
		Connection conn = driver.connect(jdbcUrl, pr);
		return conn;

	}

	/**
	 * 关闭statement
	 * 
	 * @param statement
	 * @throws SQLException
	 */
	// Statement建立了到特定数据库的连接之后，就可用该连接发送 SQL 语句
	/*
	 * Connection con = DriverManager.getConnection(url, "sunny",""); Statement
	 * stmt = con.createStatement();
	 */
	public static void closeStatement(Statement statement) throws SQLException {
		if (null != statement) {
			try {
				statement.close();
			} catch (SQLException e) {
				throw new SQLException("关闭 statement 失败");
			}
		}
	}

	/**
	 * 关闭resultSet
	 * 
	 * @throws SQLException
	 */
	// 结果集(ResultSet)是数据中查询结果返回的一种对象，可以说结果集是一个存储查询结果的对象，但是结果集并不仅仅具有存储的功能，他同时还具有操纵数据的功能，可能完成对数据的更新等。
	public static void closeResultSet(ResultSet resultSet) throws SQLException {
		if (null != resultSet) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				throw new SQLException("关闭 resultSet 失败");

			}
		}
	}

	/**
	 * 关闭connection
	 * @param connection
	 * @throws SQLException
	 */
	public static void closeConnection(Connection connection) throws SQLException{
		if(null !=connection){
			try {
				connection.close();
			} catch (SQLException e) {
				throw new SQLException("关闭 Connection 失败");
			}
		}
	}
	
	private static void closeDb(Connection coon, Statement st,ResultSet re) throws SQLException {
		closeConnection(coon);
		closeStatement(st);
		closeResultSet(re);
		// TODO Auto-generated method stub

	}
	
	
	
	/**
	 * main方法测试是否连接数据库
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		JdbcController c = new JdbcController();
		System.err.println(c.getConnection());
		Dog d = new Dog();
		
		Dogcontroller dog = new Dogcontroller();
		
		dog.add(d);
	}
}
